package com.cnap.model.stage;

import cn.hutool.core.text.CharSequenceUtil;
import com.cnap.model.strategy.SvcConstants;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <类文件描述>
 *
 * @author xc
 **/
public enum StageEnum2 {
    // 公有云 cpu
    // 注释
    ONE_ONE_PUBLIC_CPU("1-1", StageTypeEnum.CLOUD_RESOURCE.getType(), 1, "申请云资源，完成开通", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    ONE_TWO_PUBLIC_CPU("1-2", StageTypeEnum.CLOUD_RESOURCE.getType(), 2, "获取开通的云资源详情", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    TOW_ONE_PUBLIC_CPU("2-1", StageTypeEnum.SAFE_PORT.getType(), 1, "确认需要开通的端口", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    TOW_TOW_PUBLIC_CPU("2-2", StageTypeEnum.SAFE_PORT.getType(), 2, "申请端口开通", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    TOW_THREE_PUBLIC_CPU("2-3", StageTypeEnum.SAFE_PORT.getType(), 3, "等待端口开通完成", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    TOW_FOUR_PUBLIC_CPU("2-4", StageTypeEnum.SAFE_PORT.getType(), 4, "完成端口开通确认", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    THREE_ONE_PUBLIC_CPU("3-1", StageTypeEnum.K8S.getType(), 1, "检查云资源状态", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    THREE_TOW_PUBLIC_CPU("3-2", StageTypeEnum.K8S.getType(), 2, "拉取安装所需资源", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    THREE_THREE_PUBLIC_CPU("3-3", StageTypeEnum.K8S.getType(), 3, "确认安装环境", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    THREE_FOUR_PUBLIC_CPU("3-4", StageTypeEnum.K8S.getType(), 4, "安装openssh，ansible等软件", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    THREE_FIVE_PUBLIC_CPU("3-5", StageTypeEnum.K8S.getType(), 5, "配置安装参数", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    THREE_SIX_PUBLIC_CPU("3-6", StageTypeEnum.K8S.getType(), 6, "完成K8s/Prometheus镜像库同步设置", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    THREE_SEVEN_PUBLIC_CPU("3-7", StageTypeEnum.K8S.getType(), 7, "完成K8s集群节点标签设置", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    FOUR_ONE_PUBLIC_CPU("4-1", StageTypeEnum.DISTRIBUTE.getType(), 1, "完成应用镜像同步", SvcConstants.PUBLIC, SvcConstants.CPU),
    // 注释
    FOUR_TOW_PUBLIC_CPU("4-2", StageTypeEnum.DISTRIBUTE.getType(), 2, "完成应用安装部署", SvcConstants.PUBLIC, SvcConstants.CPU),

    // 注释
    UNKNOWN("--", "unknown", 0, "unknown", "unknown", "unknown"),

    // 公有云 gpu

    // 注释
    ONE_ONE_PUBLIC_GPU("1-1", StageTypeEnum.CLOUD_RESOURCE.getType(), 1, "申请云资源，完成开通", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    ONE_TWO_PUBLIC_GPU("1-2", StageTypeEnum.CLOUD_RESOURCE.getType(), 2, "获取开通的云资源详情", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    TOW_ONE_PUBLIC_GPU("2-1", StageTypeEnum.SAFE_PORT.getType(), 1, "确认需要开通的端口", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    TOW_TOW_PUBLIC_GPU("2-2", StageTypeEnum.SAFE_PORT.getType(), 2, "申请端口开通", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    TOW_THREE_PUBLIC_GPU("2-3", StageTypeEnum.SAFE_PORT.getType(), 3, "等待端口开通完成", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    TOW_FOUR_PUBLIC_GPU("2-4", StageTypeEnum.SAFE_PORT.getType(), 4, "完成端口开通确认", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    THREE_ONE_PUBLIC_GPU("3-1", StageTypeEnum.K8S.getType(), 1, "检查云资源状态", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    THREE_TOW_PUBLIC_GPU("3-2", StageTypeEnum.K8S.getType(), 2, "拉取安装所需资源", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    THREE_THREE_PUBLIC_GPU("3-3", StageTypeEnum.K8S.getType(), 3, "确认安装环境", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    THREE_FOUR_PUBLIC_GPU("3-4", StageTypeEnum.K8S.getType(), 4, "安装openssh，ansible等软件", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    THREE_FIVE_PUBLIC_GPU("3-5", StageTypeEnum.K8S.getType(), 5, "配置安装参数", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    THREE_SIX_PUBLIC_GPU("3-6", StageTypeEnum.K8S.getType(), 6, "完成K8s/Prometheus镜像库同步设置", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    THREE_SEVEN_PUBLIC_GPU("3-7", StageTypeEnum.K8S.getType(), 7, "完成docker的GPU插件安装", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    THREE_EIGHT_PUBLIC_GPU("3-8", StageTypeEnum.K8S.getType(), 8, "完成K8s安装GPU管理插件", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    THREE_NINE_PUBLIC_GPU("3-9", StageTypeEnum.K8S.getType(), 9, "完成K8s集群节点标签设置", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    FOUR_ONE_PUBLIC_GPU("4-1", StageTypeEnum.DISTRIBUTE.getType(), 1, "完成应用镜像同步", SvcConstants.PUBLIC, SvcConstants.GPU),
    // 注释
    FOUR_TOW_PUBLIC_GPU("4-2", StageTypeEnum.DISTRIBUTE.getType(), 2, "完成应用安装部署", SvcConstants.PUBLIC, SvcConstants.GPU),


    // 私有云 cpu
    // 注释
    ONE_ONE_PRIVATE_CPU("1-1", StageTypeEnum.CLOUD_RESOURCE.getType(), 1, "申请云资源，完成开通", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    ONE_TWO_PRIVATE_CPU("1-2", StageTypeEnum.CLOUD_RESOURCE.getType(), 2, "获取开通的云资源详情", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    TOW_ONE_PRIVATE_CPU("2-1", StageTypeEnum.SAFE_PORT.getType(), 1, "确认需要开通的端口", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    TOW_TOW_PRIVATE_CPU("2-2", StageTypeEnum.SAFE_PORT.getType(), 2, "申请端口开通", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    TOW_THREE_PRIVATE_CPU("2-3", StageTypeEnum.SAFE_PORT.getType(), 3, "等待端口开通完成", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    TOW_FOUR_PRIVATE_CPU("2-4", StageTypeEnum.SAFE_PORT.getType(), 4, "完成端口开通确认", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    THREE_ONE_PRIVATE_CPU("3-1", StageTypeEnum.K8S.getType(), 1, "检查云资源状态", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    THREE_TOW_PRIVATE_CPU("3-2", StageTypeEnum.K8S.getType(), 2, "拉取安装所需资源", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    THREE_THREE_PRIVATE_CPU("3-3", StageTypeEnum.K8S.getType(), 3, "确认安装环境", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    THREE_FOUR_PRIVATE_CPU("3-4", StageTypeEnum.K8S.getType(), 4, "安装openssh，ansible等软件", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    THREE_FIVE_PRIVATE_CPU("3-5", StageTypeEnum.K8S.getType(), 5, "配置安装参数", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    THREE_SIX_PRIVATE_CPU("3-6", StageTypeEnum.K8S.getType(), 6, "完成K8s/Prometheus镜像库同步设置", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    THREE_SEVEN_PRIVATE_CPU("3-7", StageTypeEnum.K8S.getType(), 7, "完成K8s集群节点标签设置", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    FOUR_ONE_PRIVATE_CPU("4-1", StageTypeEnum.DISTRIBUTE.getType(), 1, "完成应用镜像同步", SvcConstants.PRIVATE, SvcConstants.CPU),
    // 注释
    FOUR_TOW_PRIVATE_CPU("4-2", StageTypeEnum.DISTRIBUTE.getType(), 2, "完成应用安装部署", SvcConstants.PRIVATE, SvcConstants.CPU),

    // 私有云 gpu

    // 注释
    ONE_ONE_PRIVATE_GPU("1-1", StageTypeEnum.CLOUD_RESOURCE.getType(), 1, "申请云资源，完成开通", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    ONE_TWO_PRIVATE_GPU("1-2", StageTypeEnum.CLOUD_RESOURCE.getType(), 2, "获取开通的云资源详情", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    TOW_ONE_PRIVATE_GPU("2-1", StageTypeEnum.SAFE_PORT.getType(), 1, "确认需要开通的端口", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    TOW_TOW_PRIVATE_GPU("2-2", StageTypeEnum.SAFE_PORT.getType(), 2, "申请端口开通", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    TOW_THREE_PRIVATE_GPU("2-3", StageTypeEnum.SAFE_PORT.getType(), 3, "等待端口开通完成", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    TOW_FOUR_PRIVATE_GPU("2-4", StageTypeEnum.SAFE_PORT.getType(), 4, "完成端口开通确认", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    THREE_ONE_PRIVATE_GPU("3-1", StageTypeEnum.K8S.getType(), 1, "检查云资源状态", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    THREE_TOW_PRIVATE_GPU("3-2", StageTypeEnum.K8S.getType(), 2, "拉取安装所需资源", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    THREE_THREE_PRIVATE_GPU("3-3", StageTypeEnum.K8S.getType(), 3, "确认安装环境", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    THREE_FOUR_PRIVATE_GPU("3-4", StageTypeEnum.K8S.getType(), 4, "安装openssh，ansible等软件", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    THREE_FIVE_PRIVATE_GPU("3-5", StageTypeEnum.K8S.getType(), 5, "配置安装参数", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    THREE_SIX_PRIVATE_GPU("3-6", StageTypeEnum.K8S.getType(), 6, "完成K8s/Prometheus镜像库同步设置", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    THREE_SEVEN_PRIVATE_GPU("3-7", StageTypeEnum.K8S.getType(), 7, "完成docker的GPU插件安装", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    THREE_EIGHT_PRIVATE_GPU("3-8", StageTypeEnum.K8S.getType(), 8, "完成K8s安装GPU管理插件", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    THREE_NINE_PRIVATE_GPU("3-9", StageTypeEnum.K8S.getType(), 9, "完成K8s集群节点标签设置", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    FOUR_ONE_PRIVATE_GPU("4-1", StageTypeEnum.DISTRIBUTE.getType(), 1, "完成应用镜像同步", SvcConstants.PRIVATE, SvcConstants.GPU),
    // 注释
    FOUR_TOW_PRIVATE_GPU("4-2", StageTypeEnum.DISTRIBUTE.getType(), 2, "完成应用安装部署", SvcConstants.PRIVATE, SvcConstants.GPU),


    ;
    /**
     * 云商 public private
     */
    private final String cloudScope;
    /**
     * cpu  gpu
     */
    private final String arch;

    private final String detailType;
    private final String parentType;
    private final int order;
    private final String message;


    StageEnum2(String detailType, String parentType, int order, String message, String cloudScope, String type) {
        this.detailType = detailType;
        this.parentType = parentType;
        this.order = order;
        this.message = message;
        this.cloudScope = cloudScope;
        this.arch = type;
    }

    /**
     * get
     *
     * @return CloudScope
     */
    public String getCloudScope() {
        return cloudScope;
    }

    /**
     * get
     *
     * @return Type
     */
    public String getArch() {
        return arch;
    }

    /**
     * get
     *
     * @return DetailType
     */
    public String getDetailType() {
        return detailType;
    }

    /**
     * get
     *
     * @return ParentType
     */
    public String getParentType() {
        return parentType;
    }

    /**
     * get
     *
     * @return Order
     */
    public int getOrder() {
        return order;
    }

    /**
     * get
     *
     * @return Message
     */
    public String getMessage() {
        return message;
    }

    /**
     * 获取当前枚举的前一个枚举值
     *
     * @return 结果
     */
    public StageEnum2 previous() {
        int ordinal = this.ordinal();
        StageEnum2[] stageTypeEnums = StageEnum2.values();
        for (StageEnum2 stageTypeEnum : stageTypeEnums) {
            if (stageTypeEnum.ordinal() == ordinal - 1) {
                return stageTypeEnum;
            }
        }
        return UNKNOWN;
    }

    /**
     * 获取当前枚举的后一个枚举值
     *
     * @return 结果
     */
    public StageEnum2 next() {
        int ordinal = this.ordinal();
        StageEnum2[] stageTypeEnums = StageEnum2.values();
        for (StageEnum2 stageTypeEnum : stageTypeEnums) {
            if (stageTypeEnum.ordinal() == ordinal + 1) {
                return stageTypeEnum;
            }
        }
        return UNKNOWN;
    }

    /**
     * 根据类型获取枚举
     *
     * @param cloudType private public
     * @param arch      cpu gpu
     * @return 结果
     */
    public static List<StageEnum2> getStageEnum2WithParam(String cloudType, String arch) {
        return Arrays.stream(StageEnum2.values())
                .filter(stageEnum2 -> stageEnum2.getCloudScope().equals(cloudType) && stageEnum2.getArch().equals(arch))
                .collect(Collectors.toList());
    }

    /**
     * 根据类型获取枚举
     *
     * @param detailType 类型
     * @return 结果
     */
    public static StageEnum2 getStageEnum2WithParam(String detailType, String cloudType, String arch) {
        if (CharSequenceUtil.isEmpty(detailType)) {
            return UNKNOWN;
        }
        StageEnum2[] stageTypeEnums = StageEnum2.values();
        for (StageEnum2 stageTypeEnum : stageTypeEnums) {
            if (stageTypeEnum.getDetailType().equals(detailType) && stageTypeEnum.getCloudScope().equals(cloudType)
                    && stageTypeEnum.getArch().equals(arch)) {
                return stageTypeEnum;
            }
        }
        return UNKNOWN;
    }

    /**
     * 根据云商和架构选择对应的第一个阶段
     *
     * @param cloudType public private
     * @param arch      cpu gpu
     * @return 第一阶段
     */
    public static StageEnum2 getFirstStageEnum2ByCloudTypeAndArch(String cloudType, String arch) {
        if (SvcConstants.PUBLIC.equals(cloudType) && SvcConstants.CPU.equals(arch)) {
            // 公有云 cpu
            return ONE_ONE_PUBLIC_CPU;
        } else if (SvcConstants.PUBLIC.equals(cloudType) && SvcConstants.GPU.equals(arch)) {
            // 公有云 gpu
            return ONE_ONE_PUBLIC_GPU;
        } else if (SvcConstants.PRIVATE.equals(cloudType) && SvcConstants.CPU.equals(arch)) {
            // 私有云 CPU
            return ONE_ONE_PRIVATE_CPU;
        } else {
            // 私有云 GPU
            return ONE_ONE_PRIVATE_GPU;
        }
    }

    /**
     * 根据云商和架构选择对应的开始部署的阶段 3-7
     *
     * @param cloudType public private
     * @param arch      cpu gpu
     * @return 第一阶段
     */
    public static StageEnum2 get37StageEnum2ByCloudTypeAndArch(String cloudType, String arch) {
        if (SvcConstants.PUBLIC.equals(cloudType) && SvcConstants.CPU.equals(arch)) {
            // 公有云 cpu
            return THREE_SEVEN_PUBLIC_CPU;
        } else if (SvcConstants.PUBLIC.equals(cloudType) && SvcConstants.GPU.equals(arch)) {
            // 公有云 gpu
            return THREE_NINE_PUBLIC_GPU;
        } else if (SvcConstants.PRIVATE.equals(cloudType) && SvcConstants.CPU.equals(arch)) {
            // 私有云 CPU
            return THREE_SEVEN_PRIVATE_CPU;
        } else {
            // 私有云 GPU
            return THREE_NINE_PRIVATE_GPU;
        }
    }

    /**
     * 根据云商和架构选择对应的部署的阶段 4-1, 4-2
     *
     * @param cloudScope public private
     * @param arch       CPU GPU
     * @return 部署阶段
     */
    public static List<StageEnum2> getDistributeStageEnum2ByCloudTypeAndArch(String cloudScope, String arch) {
        if (SvcConstants.PUBLIC.equals(cloudScope) && SvcConstants.CPU.equals(arch)) {
            // 公有云 cpu
            return Arrays.asList(FOUR_ONE_PUBLIC_CPU, FOUR_TOW_PUBLIC_CPU);
        } else if (SvcConstants.PUBLIC.equals(cloudScope) && SvcConstants.GPU.equals(arch)) {
            // 公有云 gpu
            return Arrays.asList(FOUR_ONE_PUBLIC_GPU, FOUR_TOW_PUBLIC_GPU);
        } else if (SvcConstants.PRIVATE.equals(cloudScope) && SvcConstants.CPU.equals(arch)) {
            // 私有云 cpu
            return Arrays.asList(FOUR_ONE_PRIVATE_CPU, FOUR_TOW_PRIVATE_CPU);
        } else {
            // 私有云 gpu
            return Arrays.asList(FOUR_ONE_PRIVATE_GPU, FOUR_TOW_PRIVATE_GPU);
        }
    }
}
